<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('虚拟用况模式选择')" />
    <th:block th:include="include :: select2-css" />
    <style type="text/css">

    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-interface-add">
            <input type="hidden" id="id" name="id" th:value="${virtualUsage.id}">

            <div class="form-group">
                <label class="col-sm-5 control-label">约定模式：</label>
                <div class="col-sm-6">
                    <select name="model" id="model" class="form-control m-b" th:with="type=${@dict.getType('virtualUsageModel')}">
                        <option value="">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="${virtualUsage.model}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-5 control-label">约定模式备注：</label>
                <div class="col-sm-6">
                    <p class="form-control-static" id="remark"></p>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-5 control-label">自定义模式：</label>
                <div class="col-sm-6">
                    <input type="text" name="selfModel2" class="form-control" th:value="${virtualUsage.selfModel}" readonly/>
                </div>
            </div>

            <div class="form-group dictDiv" id="dictCategoryDiv">
                <label class="col-sm-5 control-label">字典分类：</label>
                <div class="col-sm-6">
                    <select name="dictCategory" id="dictCategory" class="form-control m-b">
                        <option value="">请选择</option>
                        <option th:each="dict : ${dictCategoryList}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}" th:field="${dictCategory}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group dictDiv" id="dictTypeDiv">
                <label class="col-sm-5 control-label">字典：</label>
                <div class="col-sm-6">
                    <select name="dictType" id="dictType" class="form-control m-b" >
                        <option th:each="dict : ${dictTypeList}" th:text="${dict.dictName}" th:value="${dict.dictType}"
                                th:field="${dictType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group dictDiv" id="dictValueDiv">
                <label class="col-sm-5 control-label">字典值：</label>
                <div class="col-sm-6">
                    <select name="selfModel" id="selfModel" class="form-control m-b" >
                        <option th:each="dict : ${dictValueList}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                                th:field="${selfModel}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-5 control-label">出参判空策略：</label>
                <div class="col-sm-6">
                    <select name="isAllowEmpty" id="isAllowEmpty" class="form-control m-b" th:with="type=${@dict.getType('isAllowEmpty')}">
                        <option value="">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="${virtualUsage.isAllowEmpty}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group errorDiv" style="display: none">
                <label class="col-sm-5 control-label">错误码分类：</label>
                <div class="col-sm-6">
                    <div class="input-group">
                        <input id="treeId" name="errorCodeCategoryId" type="hidden" th:value="${virtualUsage.errorCodeCategoryId}"/>
                        <input class="form-control" type="text" onclick="selectErrorCodeCategoryTree()" id="treeName" readonly="true" th:value="${errorCodeCategoryName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group errorDiv" style="display: none">
                <label class="col-sm-5 control-label">错误码：</label>
                <input name="errorCodeSet" type="hidden" th:value="${virtualUsage.errorCode}"/>
                <div class="col-sm-6" id="errorCodeDiv">
                    <select class="form-control" name="errorCode" id="errorCode">
                        <option value="0">--请选择--</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "interfaceinfo/virtualUsage";
        $("#form-interface-add").validate({
            focusCleanup: true
        });
        var virtualUsageModel = [[${@dict.getType('virtualUsageModel')}]];

        $(function () {
            var model = $('#model').val();
            if (model == 'generateSemanticInstance') {
                $("#selfModelDiv").show();
            } else {
                $("#selfModelDiv").hide();
            }
            $('#model').on('select2:select', function (e) {
                var value = $('#model').select2('val');
                for (var i=0; i<virtualUsageModel.length; i++) {
                    if (virtualUsageModel[i].dictValue == value) {
                        $("#remark").text(virtualUsageModel[i].remark);
                        break;
                    }
                }
                // if (value == 'generateSemanticInstance') {
                //     $("#selfModelDiv").show();
                // } else {
                //     $('#selfModel').select2('val', [""]);
                //     $("#selfModelDiv").hide();
                // }
            });
            var model = $('#model').select2('val');
            if (model) {
                for (var i=0; i<virtualUsageModel.length; i++) {
                    if (virtualUsageModel[i].dictValue == model) {
                        $("#remark").text(virtualUsageModel[i].remark);
                        break;
                    }
                }
            }

            bindDictCategoryEvent();
            bindDictTypeEvent();
        })
        function submitHandler() {
            if ($.validate.form()) {
                var config = {
                    url: prefix + "/editModel",
                    type: "POST",
                    dataType: "json",
                    async: false,
                    data: $('#form-interface-add').serialize(),
                    success: function (result) {

                    }
                };
                $.ajax(config);
            }
        }

        function resetHandler() {
            var config = {
                url: prefix + "/editModel?id="+$("#id").val(),
                type: "POST",
                dataType: "json",
                async: false,
                success: function (result) {

                }
            };
            $.ajax(config);
        }
        var dictCategory = [[${dictCategory}]];
        var dictType = [[${dictType}]];
        var selfModel = [[${selfModel}]];

        function bindDictCategoryEvent() {
            if (dictCategory) {
                var arr = [];
                arr.push(dictCategory);
                $('#dictCategory').select2('val', arr);
            }
            if (dictType) {
                var arr = [];
                arr.push(dictType);
                $('#dictType').select2('val', arr);
            }
            if (selfModel) {
                var arr = [];
                arr.push(selfModel);
                $('#selfModel').select2('val', arr);
            }
            $('#dictCategory').on('select2:select', function (e) {
                var value = $('#dictCategory').select2('val');
                $("#dictType").empty();
                $("#dictValue").empty();
                var url = ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/getDictListByCategory?category=" + value;
                var config = {
                    url: url,
                    type: "get",
                    dataType: "json",
                    data: null,
                    success: function (result) {
                        var data = result.data;
                        if (data) {
                            for (var i = 0; i < data.length; i++) {
                                $("#dictType").append("<option value='" + data[i].dictType + "'>" + data[i].dictName + "</option>");
                            }
                            queryDictValue(data[0].dictType);
                            //bindDictTypeEvent();
                        }
                    }
                };
                $.ajax(config);

            });
        }

        function bindDictTypeEvent() {
            $('#dictType').on('select2:select', function (e) {
                var value = $('#dictType').select2('val');

                queryDictValue(value);
            });
        }

        function queryDictValue(value) {
            $("#selfModel").empty();
            var url = ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryDict?dictType=" + value;
            var config = {
                url: url,
                type: "post",
                dataType: "json",
                data: null,
                success: function (result) {
                    var data = result.data;
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            $("#selfModel").append("<option value='" + data[i].dictValue + "'>" + data[i].dictLabel + "</option>");
                        }
                    }
                }
            };
            $.ajax(config);
        }



        /*错误码分类-新增-选择父错误码分类树*/
        function selectErrorCodeCategoryTree() {
            var treeId = $("#treeId").val();
            if (!treeId) {
                treeId = "";
            }
            var options = {
                title: '错误码分类选择',
                width: "380",
                url:  ctx + "errorCodeCategory/errorCodeCategory" + "/selectErrorCodeCategoryTree/" + treeId,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
            queryErrorCodeList();
        }

        function queryErrorCodeList() {
            var treeId = $("#treeId").val();
            let config = {
                url: ctx + "errorCode/errorCode/queryErrorCodeById?errorCodeCategoryId=" + treeId,
                type: "get",
                dataType: "json",
                data: null,
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function (result) {
                    $.modal.closeLoading();
                    if (result.code == web_status.SUCCESS) {
                        var selectTplHtml = $("#selectTpl").html();
                        result.selectId= $("input[name='errorCodeSet']").val()||''
                        laytpl(selectTplHtml).render(result, function (html) {
                            $("#errorCodeDiv").empty();
                            $("#errorCodeDiv").append(html);
                        });
                        return false;
                    }
                    if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                        return false;
                    }
                    $.modal.alertError(result.msg);
                }
            };
            $.ajax(config);
        }

        $(function () {
            var treeId = $("#treeId").val();
            if (treeId) {
                queryErrorCodeList();
            }

            var value = $("#isAllowEmpty").val();
            if (value == 1 || value == '1') {
                $(".errorDiv").show();
            } else {
                $(".errorDiv").hide();
            }

            $("#isAllowEmpty").change(function () {
                var value = $(this).val();
                if (value == 1 || value == '1') {
                    $(".errorDiv").show();
                } else {
                    $(".errorDiv").hide();
                }
            })
        })
    </script>

    <script id="selectTpl" type="text/html">
        <select class="form-control errorCodeValue" name="errorCode">
            <option value="0">--请选择--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.errorCode}}"
                    {{#if(a.errorCode==d.selectId){}}selected{{# }}}>{{a.errorCode}}/{{a.errorCodeDetail}}</option>
            {{# }); }}
        </select>
    </script>
</body>
</html>